<?php
include_once 'MySQLDAO.php';

/**
 * Definition of Class Country
 * This class represents a data access object for the database table 'country'.
 */
class Country {


	public $dao = null;

	private $i = 1;

	/**
	 * associated database field: countryCode, type: varchar, is nullable: false, character set: utf8, max length: 10
	 */
	private $countryCode  = 0;

	/**
	 * associated database field: countryName, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $countryName;

	/**
	 * associated database field: countryCurrency, type: varchar, is nullable: true, character set: utf8, max length: 20
	 */
	private $countryCurrency;



	/**
	 * Default constructor
	 * @param value some value
	 */
	function __construct(){
		$this->dao = new MySQLDao("Country");
	}


	/**
	 * Initialize the business object with data read from the DB.
	 * @param row database row array containing the values from one read record
	 */
	public function init($row){
		if(isset($row['countryCode'])) $this->countryCode = $row['countryCode'];
		if(isset($row['countryName'])) $this->countryName = $row['countryName'];
		if(isset($row['countryCurrency'])) $this->countryCurrency = $row['countryCurrency'];
	}


	/**
	 * Returns the string representation for this object
	 * @param escapeQuotationMarks boolean value to indicate if quotation marks in value strings should be escaped, default value is true
	 * @return String String representation of Country
	 */
	public function toString($escapeQuotationMarks=true){
		$s = '[';
		$s .= 'countryCode: '.(($escapeQuotationMarks===true) ? addslashes($this->countryCode) : $this->countryCode).', ';
		$s .= 'countryName: '.(($escapeQuotationMarks===true) ? addslashes($this->countryName) : $this->countryName).', ';
		$s .= 'countryCurrency: '.(($escapeQuotationMarks===true) ? addslashes($this->countryCurrency) : $this->countryCurrency);
		$s .= ']';
		return $s;
	}


	/**
	 * Returns the JSON (JavaScript Object Notation) string representation for this object
	 * @return String JSON representation of Country
	 */
	public function toJSONString(){
		$fieldValues = array ("countryCode"=>$this->countryCode, "countryName"=>$this->countryName, "countryCurrency"=>$this->countryCurrency);
		return json_encode($fieldValues);
	}


	/**
	 * Returns a HTML representation of this object using DIVs for formatting via css
	 * @param escapeHTML boolean value to indicate if value strings should be html escaped, default value is true
	 * @return String the HTML representation of the Country object
	 */
	public function toHTMLString($escapeHTML=true){
		$htmlCode = "<div class=\"country\">\n";
		$htmlCode .= '	<div class="country_countryCode">'.(($escapeHTML===true) ? htmlspecialchars($this->countryCode) : $this->countryCode)."</div>\n";
		$htmlCode .= '	<div class="country_countryName">'.(($escapeHTML===true) ? htmlspecialchars($this->countryName) : $this->countryName)."</div>\n";
		$htmlCode .= '	<div class="country_countryCurrency">'.(($escapeHTML===true) ? htmlspecialchars($this->countryCurrency) : $this->countryCurrency)."</div>\n";
		$htmlCode .= "</div>\n";
		return $htmlCode;
	}
	/*
	public function getData($where) {
		$rows = $this->dao->getData($where);
		return $rows;
	}*/


	public static function loadAll(){
		$country = new Country();
		$rows = $country->dao->getData("");
		$countrys = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Country();
			$d->init($rows[$index]);
			array_push($countrys,$d);
		}
		return $countrys;
	}


	/**
	 * Load the Country uniquely by its primary key.
	 * @param countryCode the primary key
	 * @return Country Instance of {@link Country}
	 */
	public static function loadUnique($countryCode){
		$country = new Country();
		$rows = $country->dao->getData("`countryCode`='$countryCode'");
		$country->init($rows[0]);
		return $country;
	}


	/**
	 * Insert this object into the DB
	 * @return number new id (auto increment value) genereated
	 */
	public function insert(){
		$list = array("countryCode"=>$this->countryCode, "countryName"=>$this->countryName, "countryCurrency"=>$this->countryCurrency);
		return $this->dao->insertRecord($list);
	}


	/**
	 * Update this object into the DB
	 * @return number the number of updated records
	 */
	public function update(){
		$list = array("countryCode"=>$this->countryCode, "countryName"=>$this->countryName, "countryCurrency"=>$this->countryCurrency);
		$where = "`countryCode`='$countryCode'";
		return $this->dao->updateRecord($list,$where);
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initPOST(){
		if (isset($_POST['countryCode'])){
			$this->countryCode = $_POST['countryCode'];
		}
		if (isset($_POST['countryName'])){
			$this->countryName = $_POST['countryName'];
		}
		if (isset($_POST['countryCurrency'])){
			$this->countryCurrency = $_POST['countryCurrency'];
		}
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initGET(){
		if (isset($_GET['countryCode'])){
			$this->countryCode = $_GET['countryCode'];
		}
		if (isset($_GET['countryName'])){
			$this->countryName = $_GET['countryName'];
		}
		if (isset($_GET['countryCurrency'])){
			$this->countryCurrency = $_GET['countryCurrency'];
		}
	}


	/**
	 * Returns the value from the database field countryCode.
	 * In the database the field countryCode is of type varchar.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getCountryCode(){
		return $this->countryCode;
	}


	/**
	 * Set the value from the database field countryCode.
	 * In the database the field countryCode is of type varchar.
	 * The database field is NOT nullable.
	 * @param countryCode String with the value for the field to be set
	 */
	public function setCountryCode($countryCode){
		if(strlen($countryCode) > 10) throw new Exception("countryCode does not allow strings longer than 10 characters. The submitted string contains ".strlen($countryCode)." characters.");
		$this->countryCode = $countryCode;
	}


	/**
	 * Returns the value from the database field countryName.
	 * In the database the field countryName is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCountryName(){
		return $this->countryName;
	}


	/**
	 * Set the value from the database field countryName.
	 * In the database the field countryName is of type varchar.
	 * The database field is  nullable.
	 * @param countryName String with the value for the field to be set
	 */
	public function setCountryName($countryName){
		if(strlen($countryName) > 45) throw new Exception("countryName does not allow strings longer than 45 characters. The submitted string contains ".strlen($countryName)." characters.");
		$this->countryName = $countryName;
	}


	/**
	 * Returns the value from the database field countryCurrency.
	 * In the database the field countryCurrency is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCountryCurrency(){
		return $this->countryCurrency;
	}


	/**
	 * Set the value from the database field countryCurrency.
	 * In the database the field countryCurrency is of type varchar.
	 * The database field is  nullable.
	 * @param countryCurrency String with the value for the field to be set
	 */
	public function setCountryCurrency($countryCurrency){
		if(strlen($countryCurrency) > 20) throw new Exception("countryCurrency does not allow strings longer than 20 characters. The submitted string contains ".strlen($countryCurrency)." characters.");
		$this->countryCurrency = $countryCurrency;
	}
}
?>